package allinpay

import (
    //"fmt"
	"errors"
	"strconv"
	"encoding/json"
	"github.com/astaxie/beego"
)

type PayReq struct {
	Cusid         string `form:"cusid"`        //商户号
	Appid         string `form:"appid"`        //应用ID
	Version       string `form:"version"`      //版本号
	Trxamt        string `form:"trxamt"`       //交易金额
	Reqsn         string `form:"reqsn"`        //商户交易单号
	Paytype       string `form:"paytype"`      //交易方式
	Randomstr     string `form:"randomstr"`    //随机字符串
	Body          string `form:"body"`          //订单标题
	Remark        string `form:"remark"`        //备注信息
	Validtime     string `form:"validtime"`     //有效时间
	Acct          string `form:"acct"`          //支付平台用户标识
	NotifyUrl     string `form:"notify_url"`    //交易结果通知地址
	LimitPay      string `form:"limit_pay"`     //支付限制
	SubAppid      string `form:"sub_appid"`     //微信子appid
	GoodsTag      string `form:"goods_tag"`     //订单优惠标识
	Benefitdetail string `form:"benefitdetail"` //优惠信息
	Subbranch     string `form:"subbranch"`     //门店号
	Cusip         string `form:"cusip"`         //终端ip
	Idno          string `form:"idno"`          //证件号
	Truename      string `form:"truename"`      //付款人真实姓名
	Asinfo        string `form:"asinfo"`        //分账信息
	Fqnum         string `form:"fqnum"`         //花呗分期
	Signtype      string `form:"signtype"`      //签名方式
	Sign          string `form:"sign"`          //签名
}

type PayRsp struct {
	Retcode   string `json:"retcode"`
	Retmsg    string `json:"retmsg"`
	Cusid     string `json:"cusid"`
	Appid     string `json:"appid"`
	Trxid     string `json:"trxid"`
	Chnltrxid string `json:"chnltrxid"`
	Reqsn     string `json:"reqsn"`
	Randomstr string `json:"randomstr"`
	Trxstatus string `json:"trxstatus"`
	Fintime   string `json:"fintime"`
	Errmsg    string `json:"errmsg"`
	Payinfo   string `json:"payinfo"`
	Sign      string `json:"sign"`
}

type PayInfo struct {
	AppId     string `json:"appId"`
	TimeStamp string `json:"timeStamp"`
	NonceStr  string `json:"nonceStr"`
	Package   string `json:"package"`
	SignType  string `json:"signType"`
	PaySign   string `json:"paySign"`
}

func MicroAppPay(custId,appId,orderId,body,openId,notifyUrl,subAppid,key string,trxamt int)(payInfo *PayInfo, err error){
	req := new(PayReq)
	req.Cusid = custId
	req.Appid = appId
	req.Version = "11"
	req.Trxamt = strconv.Itoa(trxamt)
	req.Reqsn = orderId
	req.Paytype = "W06"
	req.Randomstr = getNonceStr(32)
	req.Body = body
	req.Remark = ""
	//req.Validtime = 
	req.Acct = openId
	req.NotifyUrl = notifyUrl
	req.LimitPay = "no_credit"
	req.SubAppid = subAppid
	//req.GoodsTag = goodsTag
	//req.Benefitdetail = benefitdetail
	//req.Subbranch = 
	//req.Cusip = 
	//req.Idno = 
	//req.Truename = 
	//req.Asinfo = 
	//req.Fqnum = 
	req.Signtype = "MD5"
	reqURL := "https://vsp.allinpay.com/apiweb/unitorder/pay"
	sdk := NewSDK(reqURL, key)
	rsp := new(PayRsp)
	_, err = sdk.Request(req, rsp)
	if err != nil {
		beego.Debug("AllPayment MicroAppPay err:", err.Error())
		return
	}
	if rsp.Retcode != "SUCCESS" {
	   err = errors.New(rsp.Retmsg)
	}
	payInfo = new(PayInfo)
	err = json.Unmarshal([]byte(rsp.Payinfo), payInfo)
	return
}
